package org.aceor.funny.common.monitor;

import com.google.common.collect.Maps;
import org.aceor.funny.common.monitor.handler.*;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by lxue on 16/4/5.
 */
public class MonitorPointExecutor {
    private static final Executor cacheExecutor = Executors.newCachedThreadPool();

    private static final Map<MonitorPoint, MonitorPointHandler> handlers = Maps.newHashMap();


    static {
        handlers.put(MonitorPoint.CONNECTION_CONTEXT, new ConnectionContextHandler());

        handlers.put(MonitorPoint.EXECUTE_SQL, new ExecuteSqlHandler());

        handlers.put(MonitorPoint.GET_CONNECTION, new GetConnectionHandler());

        handlers.put(MonitorPoint.PARSE_SQL, new ParseSqlHandler());

        handlers.put(MonitorPoint.COMMON_EXECUTE, new CommonExecuteHandler());
    }

    public static void execute(final MonitorPoint point, MonitorPointHandler monitorPointHandler, final long costTime, final String[] params) {
        if (!handlers.containsKey(point) && null == monitorPointHandler) {
            throw new IllegalArgumentException("没有对应的监控执行器");
        }
        MonitorPointHandler handler =
                null == monitorPointHandler ? handlers.get(point) : monitorPointHandler;
        cacheExecutor.execute(new Runnable() {
            public void run() {
                handler.print(costTime, params);
            }
        });
    }
}
